Sprintf = require("sprintf").sprintf
Timeago = require("timeago")
ScopedClient = require "scoped-http-client"

class Formatter
  constructor: (@deployment, @extras, @application) ->

class WhereFormatter extends Formatter
  message: ->
    output  = "Environments for #{@deployment.name}\n"
    output += "-----------------------------------------------------------------\n"
    output += Sprintf "%-15s\n", "Environment"
    output += "-----------------------------------------------------------------\n"

    for environment in @deployment.environments
      output += "#{environment}\n"
    output += "-----------------------------------------------------------------\n"

    output

class LatestFormatter extends Formatter
  delimiter: ->
    "-----------------------------------------------------------------------------------\n"
  message: ->
    output  = "Recent #{@deployment.env} Deployments for #{@deployment.name}\n"
    output += @delimiter()
    output += Sprintf "%-18s | %-21s | %-12s | %-38s\n", "Who", "What", "Waste", "When"
    output += @delimiter()
    for build in @extras[0..10]
      #jobLink     = "<a href=\"#{build.url}\">#[#{build.number}]</a>" "[#{build.number}](#{build.url})"
      jobLink   = "#[#{build.number}](#{build.result})"
      result    = build.result
      duration  = build.duration / 1000 / 60
      duration  = duration.toFixed 2
      userName = "author"
      #timestamp = Sprintf "%18s / %-21s", Timeago(build.timestamp/1000), build.timestamp
      timestamp = new Date(build.timestamp).toLocaleString().replace(/:\d{1,2}$/, ' ')

      output += Sprintf "%-18s | %-21s | %-12s | %-38s\n", userName, jobLink, duration + "min", timestamp
    output += @delimiter()
    output

  # @number = 0
  # GetUserName: (index, callback) ->
  #   userToken = process.env.HUBOT_JENKINS_TOKEN
  #   hostName = process.env.HUBOT_JENKINS_API
  #   ScopedClient.create("#{hostName}/job/#{@deployment.name}/#{@number}/api/json?pretty=true&tree=actions%5bcauses%5buserId,userName%5d%5d%7b1%7d").
  #     header("User-Agent", "hubot-deploy-v0.1").
  #     header("Authorization", "Basic #{@userToken}").
  #     #auth("#{@userToken}", "").
  #     post() (err, res, body) ->
  #       bodyJson = JSON.parse body
          
  #       if bodyJson?
  #         if bodyJson.actions[0].causes[0]?
  #           userName = bodyJson.actions[0].causes[0].userName
  #           console.log "username====" + userName
  #           callback(userName)

  # GetUserName1: (cnt, index, names, extras) =>
  #   if index >= cnt
  #     names
  #   else
  #     console.log "==number=#{extras[index].number}"
  #     userToken = process.env.HUBOT_JENKINS_TOKEN
  #     hostName = process.env.HUBOT_JENKINS_API
  #     ScopedClient.create("#{hostName}/job/#{@deployment.name}/#{extras[index].number}/api/json?pretty=true&tree=actions%5bcauses%5buserId,userName%5d%5d%7b1%7d").
  #       header("User-Agent", "hubot-deploy-v0.1").
  #       header("Authorization", "Basic #{@userToken}").
  #       post() (err, res, body) ->
  #         bodyJson = JSON.parse body
            
  #         if bodyJson?
  #           if bodyJson.actions[0].causes[0]?
  #             userName = bodyJson.actions[0].causes[0].userName
  #             console.log "username====" + userName
  #             names[index] = userName
  #             index++
  #             GetUserName1(cnt, index, names, extras)
              
exports.WhereFormatter  = WhereFormatter
exports.LatestFormatter = LatestFormatter
